
#include "zen_vap_scene.h"
#include "zen_vap_activity.h"

namespace Zen { namespace Vap {
	
	Scene::Scene()
	{
	}
	
	void Scene::draw()
	{
		if(m_nodes.empty()) return;
		
		DrawStack::GuardMatrix __gm(&m_camera.getMatrix(), m_camera.getMatrixVersion());
		
		this->sortNodes();
		this->drawNodes();
	}
	
	Camera & Scene::getCamera()
	{
		return m_camera;
	}
	
	Scene2d::Scene2d()
	{
		
	}
	void Scene2d::projectView(float x, float y, float width, float height)
	{
		m_camera.setMatrix(Mat4MakeOrtho(x, width+x, y, height+y, -1, 1), 0);
	}
	
	Scene3d::Scene3d()
	{
		auto vs = Activity::Get()->getRealViewSize();
		auto aspect = vs.w / vs.h;
		auto m1 = Mat4MakePerspective(90 * (M_PI / 180.0f), aspect, 3, 100);
		m_camera.setMatrix(m1, 0);
	}
	
#if ZEN_DRAW_GLES
	void Scene2d::draw()
	{
		auto s = Render::Get();
		
		s->enableDepthTest(false);
		s->cullFace(eFace::None);
		
		Scene::draw();
	}
	void Scene3d::draw()
	{
		auto s = Render::Get();
		
		s->clearDepthBuffer();
		s->enableDepthTest(true);
		s->setDepthFunc(GL::eDepthFunc::Less);
		s->cullFace(eFace::Back);
		
		Scene::draw();
	}
#elif ZEN_DRAW_METAL
	void Scene2d::draw()
	{
#error "TODO"
		auto s = Render::Get();
		s->enableDepthTest(false);
		s->cullFace(eFace::None);
		
		Scene::draw();
	}
	void Scene3d::draw()
	{
#error "TODO"
		auto s = Render::Get();
		//		s->clearDepthBuffer();
		s->enableDepthTest(1);
		//		s->setDepthFunc(GL::eDepthFunc::Less);
		s->cullFace(eFace::Back);
		
		Scene::draw();
	}
#endif
	
}}
